package com.pinganfu.mock.test;

/*import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
 
*//**
 * 
* <p>Title: JSON-XML转换工具</p>
* <p>desc:
* <p>Copyright: Copyright(c)Gb 2012</p>
* @author http://www.ij2ee.com
* @time 上午8:20:40
* @version 1.0
* @since
 *//*
public class test {
    private static final String STR_JSON = "{\"name\":\"Michael\",\"address\":{\"city\":\"Suzou\",\"street\":\" Changjiang Road \",\"postcode\":100025},\"blog\":\"http://www.ij2ee.com\"}";
    public static String xml2JSON(String xml){
        return new XMLSerializer().read(xml).toString();
    }
     
    public static String json2XML(String json){
        JSONObject jobj = JSONObject.fromObject(json);
        String xml =  new XMLSerializer().write(jobj);
        return xml;
    }
     
    public static void main(String[] args) {
        String xml = json2XML(STR_JSON);
        System.out.println("xml = "+xml);
        String json = xml2JSON(xml);
        System.out.println("json="+json);
    }
}
*/

import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.FileReader;  
import java.io.FileWriter;  
import java.io.RandomAccessFile;

import org.json.JSONObject;  
import org.json.XML;  
  
public class test {    
    public static void main(String[] args) {    
        BufferedReader buffRed=null;    
        BufferedWriter buffWri=null;    
  
        try{    
            //将XML转换成JSON    
           /* String fileName1="json.xml";    
            buffRed=new BufferedReader(new FileReader(fileName1));    
            String tempStr;    
            StringBuffer xmlStrBuff=new StringBuffer();    
            while((tempStr=buffRed.readLine())!=null)    
            xmlStrBuff.append(tempStr);    
            JSONObject syz=XML.toJSONObject(xmlStrBuff.toString());  
            System.out.println("syz="+syz.getInt("syz"));  
            System.out.println("JSON str="+syz);    */
             
            //将JSON转换成XML    
            String jsonStr="{Heros:{FBI:[{name:\"1\",age:\"24\"},{name:\"jack\",age:\"25\"}],NBA:[{name:\"tom\",sex:\"man\"},{name:\"jack\",sex:\"women\"}]}};";    
            //String jsonStr="{syz:2};";    
            String fileName2="json.xml";    
            buffWri=new BufferedWriter(new FileWriter(fileName2));    
            JSONObject jsonObj=new JSONObject(jsonStr);    
            buffWri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            buffWri.newLine();    
            buffWri.write("<ROOT>");
            buffWri.newLine();
            buffWri.write(XML.toString(jsonObj));
            buffWri.newLine();    
            buffWri.write("</ROOT>");    
            buffWri.newLine();    
            buffWri.flush();    
            
            final RandomAccessFile randomFile = new RandomAccessFile(fileName2,"rw");
            String tmp = "";     
            while( (tmp = randomFile.readLine())!= null) {     
                System.out.println(new String(tmp.getBytes("ISO8859-1")));     
            }     
            
        }catch(Exception e){    
            e.printStackTrace();    
        }finally{    
            try{    
                if(buffRed!=null)    
                    buffRed.close();    
                if(buffWri!=null)    
                    buffWri.close();    
            }catch(Exception e){    
                e.printStackTrace();    
            }    
        }    
    }    
} 